package org.daoImpl;

import org.dao.HistoryDao;
import org.model.History;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created by tedzy on 2015/5/18.
 * History Dao Impl
 */
public class HistoryDaoImpl extends HibernateDaoSupport implements HistoryDao {
    @Override
    public int newHistory(int wikiid, int uid, String title, String content, String reason) {
        History history = new History();
        history.setWikiId(wikiid);
        history.setAuthor(uid);
        history.setTitle(title);
        history.setContent(content);
        history.setReason(reason);
        this.getHibernateTemplate().save(history);
        return 0;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<History> getHistories(int wikiid) {
        return (List<History>) this.getHibernateTemplate().find("from History where wiki_id=? order by id desc", wikiid);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<History> getAuthorHistories(int uid) {
        return (List<History>) this.getHibernateTemplate().find("from History where author=? order by id desc", uid);
    }

    @Override
    public History getHistory(int id) {
        History history = null;
        List list = this.getHibernateTemplate().find("from History where id=?", id);
        if(list.size() > 0){
            history = (History) list.get(0);
        }
        return history;
    }
}
